import { fromLonLat, commonStyle } from './utils'
import { OlFeature, OlLineString } from './type'
import { getUid } from 'ol/util'
import { Stroke, Style } from 'ol/style'
/**
 * 线
 */
class WLine {
  constructor (options) {
    const { map, path, option, extData } = options
    this.WTYPE = 'Line'
    this.defaultStyle = commonStyle(option)
    this.events = ['done', 'modifyend', 'click', 'dblclick', 'contextmenu'] // 支持的事件
    this.WEvents = new Map() // 存储事件
    this.olTarget = this.init(path, extData)
    if (map) {
      map.add(this)
    }
  }
  /**
   * 初始化 线
   * @param center 
   * @param option 
   * @param extData 
   * @returns 
   */
  init (path, extData = {}) {
    const finalPath = []
		// 进行坐标系转换
		for (let i = 0; i < path.length; i++) {
			const tmpPoint = fromLonLat(path[i])
			finalPath.push(tmpPoint)
		}
		const tmp = new OlLineString(finalPath)
		this.olTarget = new OlFeature({
			geometry: tmp
		})
		this.olTarget.set('extData', extData)
		if (extData.id) {
			this.olTarget.setId(extData.id)
		} else {
			this.olTarget.setId(this.olTarget.ol_uid)
		}
    this.olTarget.setStyle(this.defaultStyle)
    return this.olTarget
  }
  /**
   * 主动改变线段
   * @returns 
   */
  setPath (coordinate) {
    const tmpPoint = fromLonLat(coordinate)
    this.olTarget.getGeometry().appendCoordinate(tmpPoint)
  }
  getStyle () {
    return this.olTarget.getStyle()
  }
  /**
   * 获取用户自定义属性
   */
  getExtData () {
    return this.olTarget.get('extData')
  }
  /**
   * 设置用户自定义属性
   */
  setExtData (data) {
    this.olTarget.set('extData', data)
  }
  /**
   * 地图上显示线
   */
  show() {
    let source = this.olTarget.get('source')
    source.forEach(item => {
      if (this.olTarget === item) {
        item.setStyle(this.defaultStyle)
      }
    })
  }
  /**
   * 地图上隐藏线
   */
  hide() {
    let source = this.olTarget.get('source')
    source.forEach(item => {
      if (this.olTarget === item) {
        item.setStyle(null)
        let s = new Style()
        let stroke = new Stroke()
        stroke.setWidth(0)
        stroke.setColor('transparent')
        s.setStroke(stroke)
        item.setStyle(s)
      }
    })
  }
  /**
   * 设置中心线
   */
  setCenter() {
  }
  /**
   * 获取中心线
   */
  getCenter () {
    return this.olTarget.getGeometry()
  }
  /**
   * 事件注册
   * @param eventName 
   * @param callBack 
   * @returns 
   */
	on(eventName, callBack = () => {}) {
		if (!eventName || typeof eventName !== 'string') throw new Error('请传入正确的 eventName！')
		if (!this.events.includes(eventName)) return console.warn('无效的事件：' + eventName)
		const eventObject = {
			eventName: 'WLine(' + eventName + ')' + getUid(this.olTarget),
			callBack,
			handler: e => {
				e.callBack({
					type: e.eventName,
					target: this,
					event: e.event
				})
			}
		}
		// 未绑定过事件
		if (!this.WEvents.has(eventObject.eventName)) {
			if (eventObject.eventName === 'WLine(' + eventName + ')') {
				let eve = document.createEvent('HTMLEvents')
				eve.initEvent(eventName, false, true)
				eve.callBack = callBack
				eventObject.costomEvent = eve
				window.addEventListener(eventName, e => e.callBack && e.callBack(this.drawFeature))
			}else {
        if (eventName === 'contextmenu') {
          let mapTarget = this.olTarget.get('map')
          // 禁止默认右键事件
          mapTarget.getTargetElement().oncontextmenu = () => {
            return false
          }
        }
				//监听事件 - WMap 处理成 cliclk
				this.olTarget.on(eventObject.eventName, eventObject.handler)
			}
			//储存事件
			this.WEvents.set(eventObject.eventName, eventObject)
		} else {
			const currentEventObject = this.WEvents.get(eventObject.eventName)
			// 移除事件
			this.olTarget.un(currentEventObject.eventName, currentEventObject.handler)
			// 重新设置监听事件
			this.olTarget.on(currentEventObject.eventName, eventObject.handler)
			//储存新事件
			this.WEvents.set(currentEventObject.eventName, eventObject)
		}
		this.olTarget && this.olTarget.set('WEvents', this.WEvents)
	}
  /**
   * 事件移除
   * @param eventName 
   * @param callBack 
   */
	off(eventName, callBack = () => {}) {
		eventName = 'WLine(' + eventName + ')' + getUid(this.olTarget)
		if (this.WEvents.has(eventName)) {
			// 获取事件对象
			const currentEventObject = this.WEvents.get(eventName)
			// 移除事件
			this.olTarget.un(currentEventObject.eventName, currentEventObject.handler)
			// 删除事件存储
			this.WEvents.delete(eventName)
			this.olTarget && this.olTarget.set('WEvents', this.WEvents)
			callBack()
		}
	}
}

export default WLine
